 /**
 * 逻辑、功能相关描述: <br>
 *
 * @author 付湘辉  <br>
 * 编写日期：2008-12-24  <br>
 */
package com.ce.pms.custservice.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import org.springframework.util.Assert;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.dao.IChargeRecordDao;
import com.ce.pms.charge.model.BusinessCodeDomain;
import com.ce.pms.charge.model.ChargeRecordDO;
import com.ce.pms.charge.service.IBusinessCodeService;
import com.ce.pms.charge.service.IChargeRecordProcessor;
import com.ce.pms.charge.service.ITransactionService;
import com.ce.pms.charge.vo.ChargeRecordVO;
import com.ce.pms.charge.vo.CreditTransVO;
import com.ce.pms.charge.vo.DebitTransVO;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.util.BigDecimalUtil;
import com.ce.pms.custservice.dao.IBillDao;
import com.ce.pms.custservice.model.BillDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.model.HouseDomain;
import com.ce.pms.house.service.IHouseService;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;

/**
 * 	名称: com.ce.pms.custservice.service.impl.CustomerEventChargeRecordProcessorImpl <br>
 * 客服事件费用记录处理器
 *
 * @author  付湘辉  <br>
 * Created on： 2008-12-24 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class CustomerEventChargeRecordProcessorImpl extends PmsBaseServiceImpl
		implements IChargeRecordProcessor {

	private IBillDao billDao;
	
	/**
	 * 交易码Service
	 */
	private IBusinessCodeService businessCodeService;
	
	private IChargeRecordDao chargeRecordDao;
	
	private IHouseService houseService;

	 private ITransactionService transactionService;

	 /**
	    * 功能描述: 删除费用记录 <br>
		* @param chargeRecordVO - 费用记录VO
	    *
	    * Create author:付湘辉 <br>
	    * Create on:2008-12-24  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */
	public Boolean deleteChargeRecord(ChargeRecordVO chargeRecordVO) {
		throw new BusinessException("charge:charge_cannot_delete_the_custeventcharge_exception",
				BusinessException.WARNING);
	}
	
	/**
	    * 功能描述:修改费用记录  <br>
	    * @param chargeRecord - 费用记录VO
	    * @return ChargeRecordVO
	    *
	    * Create author:付湘辉 <br>
	    * Create on:2008-12-24  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why: <br>
	    */
	public ChargeRecordVO editChargeRecord(ChargeRecordVO chargeRecord) {
		Assert.notNull(chargeRecord);
		SysInfo sysInfo = new SysInfo();
		sysInfo.setModifier(chargeRecord.getModifier());
		sysInfo.setModifyDate(super.getDate());
		chargeRecord = this.editChargeRecordInner(chargeRecord, sysInfo);
		return chargeRecord;
	}
	/**
	 * 功能描述: 批量修改费用记录 <br>
	 * @param operator 操作者ID
	 * @param chargeRecordVos 费用记录数据集
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<ChargeRecordVO> editChargeRecordBatch(Long operator, List<ChargeRecordVO> chargeRecordVos) {
		Assert.notNull(chargeRecordVos);
		SysInfo sysInfo = new SysInfo();
		sysInfo.setModifier(operator);
		sysInfo.setModifyDate(super.getDate());
		for (Iterator<ChargeRecordVO> iter = chargeRecordVos.iterator(); iter.hasNext();) {
			ChargeRecordVO recordVO = iter.next();
			recordVO.setModifier(operator);
			recordVO.setModifyDate(sysInfo.getModifyDate());
			this.editChargeRecordInner(recordVO, sysInfo);
		}
		return chargeRecordVos;
	}
	/**
	    * 功能描述:处理费用记录  <br>
	    * @param accountHouseId 房屋ID
	    * @param operator 操作者
	    * @param businessCodeId - 支付交易码ID
	    * @param chargeRecords - 费用记录集
	    * @return Boolean
	    *
	    * Create author:付湘辉 <br>
	    * Create on:2008-12-24  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */
	public Boolean handleChargeRecords(Long accountHouseId, Long operator, Long businessCodeId, List<ChargeRecordVO> chargeRecords) {
		super.checkParameterIsNull("common:parameters_null_exception, ",accountHouseId,operator,businessCodeId,chargeRecords);
		checkIsCompriseMonthBalance(chargeRecords);
		
		Long payBusinessId = businessCodeId;//支付交易码
		if (!BlankUtil.isBlank(chargeRecords)) {
		   List<DebitTransVO> list = new ArrayList<DebitTransVO>();
			BigDecimal totalCredit = BigDecimalUtil.getZEROInstance();
		   for (Iterator<ChargeRecordVO> iter = chargeRecords.iterator(); iter.hasNext();) {
				ChargeRecordVO recordVO = iter.next();
				DebitTransVO debitVo = new DebitTransVO();
				this.assembleChargeRecodVO2DebitTransVO(recordVO, debitVo, accountHouseId);

				//设置应缴费用
				debitVo.setReceivablePrice(recordVO.getSum());
				totalCredit=totalCredit.add(recordVO.getSum());

				list.add(debitVo);
			}
		   //批量应收交易
			list = this.transactionService.addDebitPrincipalTransactionBatch(operator, accountHouseId, list);
			// 构造一个实收交易VO
			CreditTransVO creditVo= new CreditTransVO(accountHouseId,payBusinessId,operator,null,totalCredit,new HashSet<DebitTransVO>(list));
			//缴费处理：增加一笔实收交易
			creditVo = this.transactionService.addChargeTransactionReality(creditVo);
		}

		return true;
	}

	/**
	    * 功能描述: 月末结算 <br>
	    * @param chargeRecordVO
	    *
	    * Create author:付湘辉 <br>
	    * Create on:2008-12-24  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */
	   public ChargeRecordVO monthBalance(ChargeRecordVO chargeRecordVO){
		   Assert.notNull(chargeRecordVO);
		   DebitTransVO debitTransVO=new DebitTransVO();

		   //进行装配
		   this.assembleChargeRecodVO2DebitTransVO(chargeRecordVO, debitTransVO, chargeRecordVO.getAccountHouseId());

		   //本金入账
		   this.transactionService.addDebitPrincipalTransaction(debitTransVO);

		   return chargeRecordVO;
	   }

	/**
	 * @param billDao the billDao to set
	 */
	public void setBillDao(IBillDao billDao) {
		this.billDao = billDao;
	}

	/**
	 * @param businessCodeService the businessCodeService to set
	 */
	public void setBusinessCodeService(IBusinessCodeService businessCodeService) {
		this.businessCodeService = businessCodeService;
	}

	   /**
	 * @param chargeRecordDao the chargeRecordDao to set
	 */
	public void setChargeRecordDao(IChargeRecordDao chargeRecordDao) {
		this.chargeRecordDao = chargeRecordDao;
	}

	/**
	 * @param houseService the houseService to set
	 */
	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}
	   
	   /**
	 * @param transactionService the transactionService to set
	 */
	public void setTransactionService(ITransactionService transactionService) {
		this.transactionService = transactionService;
	}
	/**
	    * 功能描述:根据一个费用记录VO装配一个应收交易VO  <br>
	    * @param chargeRecordVO
	 * @param debitTransVO
	 * @param accountHouseId TODO
	    * @return
	    *
	    * Create author:付湘辉 <br>
	    * Create on:2008-12-29  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */
	   private DebitTransVO assembleChargeRecodVO2DebitTransVO(ChargeRecordVO chargeRecordVO,DebitTransVO debitTransVO, Long accountHouseId){
		   BeanUtil.convertVoAsDo(chargeRecordVO, debitTransVO);
		   // 应收金额
		   debitTransVO.setDebit(chargeRecordVO.getSum());
		   //设置账户所属房间ID，用于入账查找账户
		   debitTransVO.setAccountHouseId(accountHouseId);
		   return debitTransVO;
	   }

	/**
	 * 功能描述:费用记录VO2DO  <br>
	 * @param chargeRecordVO
	 * @param chargeRecordDO
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-24  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private ChargeRecordDO assembleChargeRecordV2D(ChargeRecordVO chargeRecordVO,ChargeRecordDO chargeRecordDO){

		BeanUtil.convertVoAsDo(chargeRecordVO,chargeRecordDO);
		//房屋
		if (!BlankUtil.isBlank(chargeRecordVO.getHouseId())) {
			HouseDomain house = this.houseService.getHouseById(chargeRecordVO.getHouseId());
			chargeRecordDO.setHouse(house);
		}

		//交易码
		if (!BlankUtil.isBlank(chargeRecordVO.getBusinessId())) {
			BusinessCodeDomain bizCode = this.businessCodeService.getBusinessCodeDomainById(chargeRecordVO.getBusinessId());
			chargeRecordDO.setBizCode(bizCode);
		}

		return chargeRecordDO;
	}

	/**
	 * 功能描述:  <br>
	 * @param chargeRecords
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-30  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unused")
	private void assortRecordForEnterAccountMethod(Long houseId, Long operator, Long businessCodeId,List<ChargeRecordVO> chargeRecords) {
		
	}

	/**
	 * 功能描述:检查传入的费用记录中是否含有月末结算模式的记录  <br>
	 * @param chargeRecords
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-30  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private void checkIsCompriseMonthBalance(List<ChargeRecordVO> chargeRecords) {
		for (ChargeRecordVO recordVO : chargeRecords) {
			BillDO bill = this.billDao.findById(recordVO.getBizId());
			//检查是否是月末结算模式
			if (bill.isMonthBalance()) {
				throw new BusinessException("charge:charge_not_month_balance_exception",
						BusinessException.WARNING);
			}
		}
	}
	
	/**
	 * 功能描述:修改一条费用记录 <br>
	 * @param chargeRecord
	 * @param sysInfo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private ChargeRecordVO editChargeRecordInner(ChargeRecordVO chargeRecord,SysInfo sysInfo) {
		ChargeRecordDO chargeRecordDO = this.chargeRecordDao.findById(chargeRecord.getKeyword());
		this.assembleChargeRecordV2D(chargeRecord, chargeRecordDO);
		BeanUtil.convertVoAsDo(sysInfo, chargeRecordDO.getSysInfo());
		chargeRecordDO.checkRequiredField();
		this.chargeRecordDao.updateObject(chargeRecordDO);
		return chargeRecord;
	}

}
